/**
 * 
 */
package example.rss;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import rlm.helper.StringPair;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
 * manager rss feeds and subscriptions for the demo application
 * 
 * @author robot
 */
public class RSSManager {

	private static List<String> defaultSubscriptions = new ArrayList<String>();
	static {
		defaultSubscriptions.add("http://googleblog.blogspot.com/atom.xml"); // google blog
		defaultSubscriptions.add("http://www.tuaw.com/bloggers/mike-schramm/rss.xml"); //tuaw
		defaultSubscriptions.add("http://www.engadget.com/rss.xml"); // engadget
		defaultSubscriptions.add("http://lifehacker.com/index.xml"); //lifehacker
		defaultSubscriptions.add("http://rss.slashdot.org/Slashdot/slashdot"); //slashdot
		defaultSubscriptions.add("http://gmailblog.blogspot.com/atom.xml"); //gmail blog
	}
	
	private List<URL> subscriptions;
	private List<RSSFeed> rssFeeds;
	
	public RSSManager(){
		this(defaultSubscriptions);
	}


	/**
	 * @param defaultSubscriptions2
	 * @throws MalformedURLException 
	 */
	public RSSManager(List<String> subs){
		subscriptions = new ArrayList<URL>();
		rssFeeds = new ArrayList<RSSFeed>();
		
		for(String url : subs){
			try {
				subscriptions.add(new URL(url));
			} catch (MalformedURLException e) {
				// ignore this url
				System.out.println("ignored: " + url);
			}
		}
		
	}
	

	/**
	 * @param feed
	 * @return
	 */
	private RSSFeed translate(SyndFeed feed) {
		RSSFeed my = new RSSFeed();
		my.setAuthor(feed.getAuthor());
		my.setDescription(feed.getDescription());
		my.setTitle(feed.getTitle());
		my.setDate(feed.getPublishedDate());
		
		return fillEntries(my, feed.getEntries());
	}


	/**
	 * @param my
	 * @param entries
	 * @return
	 */
	private RSSFeed fillEntries(RSSFeed feed, List entries) {
		for(Object o : entries){
			SyndEntry entry = (SyndEntry)o;
			RSSEntry my = new RSSEntry();
			my.setAuthor(entry.getAuthor());
			my.setDate(entry.getPublishedDate());
			my.setTitle(entry.getTitle());
			
			if (entry.getContents().size() > 0)
				feed.getEntries().add(fillContent(my, entry.getContents()));
			else 
				feed.getEntries().add(fillContent(my, entry.getDescription()));
		}
		return feed;
	}


	/**
	 * @param my
	 * @param description
	 * @return
	 */
	private RSSEntry fillContent(RSSEntry entry, SyndContent content) {
		String type = content.getType();
		String value = content.getValue();
		
		entry.getContentList().add(new StringPair(type, value));
		return entry;
	}


	/**
	 * @param entry
	 * @param contents
	 * @return
	 */
	public RSSEntry fillContent(RSSEntry entry, List contents) {
		for(Object o : contents){
			SyndContent content = (SyndContent)o;
			fillContent(entry, content);
		}
		return entry;
	}


	public RSSFeed fetch(URL source) throws IllegalArgumentException, FeedException, IOException {
        SyndFeedInput input = new SyndFeedInput();
        SyndFeed feed = input.build(new XmlReader(source));
        return translate(feed);
	}

	/**
	 * @return
	 */
	public List<RSSFeed> getFeeds() {
		if (rssFeeds != null && rssFeeds.size() == subscriptions.size())
			return rssFeeds;
		else {
			// work
			for(URL url : subscriptions){
				// create feed
				try {
					rssFeeds.add(fetch(url));
				} catch (Exception e) {
					System.out.println("something's wrong with the feed: "+url);
					e.printStackTrace();
				}
			}
			markStarred(rssFeeds);
			return rssFeeds;
		}
	}


	private void markStarred(Collection<RSSFeed> feeds){
		for(RSSFeed feed : feeds){
			if (feed.getTitle().contains("Gmail") || feed.getTitle().contains("life"))
				feed.setStarred(true);
		}
	}
}
